package com.youngjun.common.mybatis.page;

import com.github.pagehelper.Page;

public class PageParams {

    private final static Integer MIN_CURRENT_PAGE = 1;
    private final static Integer DEFAULT_CURRENT_PAGE = MIN_CURRENT_PAGE;
    private final static Integer DEFAULT_PAGE_SIZE = 10;
    private final static Integer MAX_PAGE_SIZE = 100;
    private final static String DEFAULT_ORDER_BY = "GMT_MODIFIED DESC, ID ASC";

    private Integer currentPage;
    private Integer pageSize;
    private String orderBy;

    private Page<?> page;

    public static PageParams defaultOrderBy() {
        return new PageParams(DEFAULT_ORDER_BY);
    }

    public static PageParams defaultPageSize() {
        return new PageParams(DEFAULT_CURRENT_PAGE, DEFAULT_PAGE_SIZE);
    }

    /**
     * @param currentPage fixValue: @DEFAULT_CURRENT_PAGE
     * @param pageSize    fixValue: @DEFAULT_PAGE_SIZE
     */
    public PageParams(Integer currentPage, Integer pageSize) {
        if (currentPage == null) {
            this.currentPage = DEFAULT_CURRENT_PAGE;
        } else {
            this.currentPage = Integer.max(MIN_CURRENT_PAGE, currentPage);
        }
        if (pageSize == null) {
            this.pageSize = DEFAULT_PAGE_SIZE;
        } else {
            this.pageSize = Integer.min(MAX_PAGE_SIZE, pageSize);
        }

        this.page = new Page<>(this.currentPage, this.pageSize, true);
        this.page.setReasonable(null);
        this.page.setPageSizeZero(null);
        this.page.setOrderByOnly(false);
        this.page.setOrderBy(null);
    }

    /**
     * @param currentPage fixValue: @DEFAULT_CURRENT_PAGE
     * @param pageSize    fixValue: @DEFAULT_PAGE_SIZE
     * @param orderBy     fixValue: @DEFAULT_ORDER_BY
     */
    public PageParams(Integer currentPage, Integer pageSize, String orderBy) {
        if (currentPage == null) {
            this.currentPage = DEFAULT_CURRENT_PAGE;
        } else {
            this.currentPage = Integer.max(MIN_CURRENT_PAGE, currentPage);
        }
        if (pageSize == null) {
            this.pageSize = DEFAULT_PAGE_SIZE;
        } else {
            this.pageSize = Integer.min(MAX_PAGE_SIZE, pageSize);
        }
        if (orderBy == null) {
            this.orderBy = DEFAULT_ORDER_BY;
        } else {
            this.orderBy = orderBy;
        }

        this.page = new Page<>(this.currentPage, this.pageSize, true);
        this.page.setReasonable(null);
        this.page.setPageSizeZero(null);
        this.page.setOrderByOnly(false);
        this.page.setOrderBy(this.orderBy);
    }

    /**
     * @param orderBy fixValue: @DEFAULT_ORDER_BY
     */
    public PageParams(String orderBy) {
        if (orderBy == null) {
            this.orderBy = DEFAULT_ORDER_BY;
        } else {
            this.orderBy = orderBy;
        }
        this.page = new Page<>();
        this.page.setOrderByOnly(true);
        this.page.setOrderBy(this.orderBy);
    }

    public Integer getCurrentPage() {
        return currentPage;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public String getOrderBy() {
        return orderBy;
    }

    public Page<?> getPage() {
        return this.page;
    }
}
